package org.dashboard.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.RootPanel;
import org.dashboard.client.widget.MultiWidgetHolder;
import org.dashboard.client.widget.alarm.AlarmWidget;
import org.dashboard.client.widget.backgroundimage.BackgroundImageWidget;
import org.dashboard.client.widget.calendar.CalendarWidget;
import org.dashboard.client.widget.dailytopten.DailyTopTenErrorWidget;
import org.dashboard.client.widget.dailytopten.DailyTopTenWidget;
import org.dashboard.client.widget.errorcount.ErrorCountWidget;
import org.dashboard.client.widget.messageflow.*;
import org.dashboard.client.widget.refreshingimage.RefreshingImageWidget;
import org.dashboard.client.widget.serversup.ServersUpWidget;
import org.dashboard.shared.Props;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Dashboard implements EntryPoint {

    private final int m1 = 60 * 1000;
    private final int m15 = 15 * m1;
    private final int m5 = 5 * m1;
    private final int m2 = 2 * m1;
    private final int m30 = 30 * m1;
    private final int m60 = 60 * m1;

    /**
     * This is the entry point method.
     */
    public void onModuleLoad() {
        int height;
        int width;

        Props props = Props.instance();

        AbsolutePanel mainPanel = new AbsolutePanel();
        mainPanel.setSize(Window.getClientWidth() + "px", Window.getClientHeight() + "px");

        BackgroundImageWidget background = new BackgroundImageWidget(props.getBackgroundImageUrls(), m15);
        mainPanel.add(background);

        width = 360;
        height = 160;
        ServersUpWidget serversUp = new ServersUpWidget(width + "px", height + "px", m1);
        mainPanel.add(serversUp);
        mainPanel.setWidgetPosition(serversUp, 345, 20);

        RefreshingImageWidget burndown1 = new RefreshingImageWidget(props.getBurndownUrls()[0], "359px", "284px", m15);
        mainPanel.add(burndown1);
        mainPanel.setWidgetPosition(burndown1, 345, 210);

        width = 600;
        height = 310;
        //SERVER IS HANDLING THE UPDATE OF DATA FROM DATABASE REFRESH INTERVAL IS ONLY FOR UI UPDATE !
        NewAndProcessingWidget  newAndProcessingWidget  = new NewAndProcessingWidget(width + "px", height + "px", m1);
        mainPanel.add(newAndProcessingWidget);
        mainPanel.setWidgetPosition(newAndProcessingWidget, 1240, 20);

        width = 600;
        height = 310;
        NewMessageFlowWidget newMessageFlowWidget = new NewMessageFlowWidget(width, height, m15);
        TodayMessageCountWidget todayMessageCountWidget = new TodayMessageCountWidget(width + "px", height + "px", m5);
        //SERVER IS HANDLING THE UPDATE OF DATA FROM DATABASE REFRESH INTERVAL IS ONLY FOR UI UPDATE !
        NewAndProcessingMessageFlowWidget  newAndProcessingMessageFlowWidget = new NewAndProcessingMessageFlowWidget(width, height , m1);

        MultiWidgetHolder m1WidgetHolder = new MultiWidgetHolder(10000, newMessageFlowWidget, todayMessageCountWidget, newAndProcessingMessageFlowWidget);
        mainPanel.add(m1WidgetHolder);
        mainPanel.setWidgetPosition(m1WidgetHolder, 1240, 350);

        ProcessingFlowWidget processingFlowWidget = new ProcessingFlowWidget(width, height, m15);
        YesterdayMessageCountWidget yesterdayMessageCountWidget = new YesterdayMessageCountWidget(width + "px", height + "px", m60);
        DailyTopTenWidget dailyTopTen = new DailyTopTenWidget(width + "px", height + "px", m15);

        MultiWidgetHolder m2WidgetHolder = new MultiWidgetHolder(10000, processingFlowWidget, yesterdayMessageCountWidget, dailyTopTen);
        mainPanel.add(m2WidgetHolder);
        mainPanel.setWidgetPosition(m2WidgetHolder, 1240, 680);

        width = 490;
        height = 200;
        TotalMessageCountWidget totalMessageCountWidget  = new TotalMessageCountWidget(width + "px", height + "px", m5);
        mainPanel.add(totalMessageCountWidget);
        mainPanel.setWidgetPosition(totalMessageCountWidget, 725, 20);

        width = 450;
        height = 310;
        ErrorCountWidget errors = new ErrorCountWidget(width, height, m5);
        mainPanel.add(errors);
        mainPanel.setWidgetPosition(errors, 725, 240);

        width = 850;
        height = 150;
        DailyTopTenErrorWidget dailyTopTenErrorWidget = new DailyTopTenErrorWidget(width + "px", height + "px", m15);
        mainPanel.add(dailyTopTenErrorWidget);
        mainPanel.setWidgetPosition(dailyTopTenErrorWidget, 345, 850);

        width = 300;
        height = Window.getClientHeight() - 80;
        CalendarWidget cal = new CalendarWidget(width + "px", height + "px", m30);
        mainPanel.add(cal);
        mainPanel.setWidgetPosition(cal, 20, 20);

        AlarmWidget alarm = new AlarmWidget();
        mainPanel.add(alarm);

        serversUp.setAlarm(alarm);

        RootPanel.get("dash").add(mainPanel);
    }
}
